const { verify } = require('../helper')
const whitelist = [
  '/login',
  '/api/login'
];

module.exports = () => {
  return async (ctx, next) => {
    // 白名单
    console.log('ctx.path-----', ctx.path);
    if (whitelist.includes(ctx.path)) {
      await next();
      return;
    }


    // 用户身份判断
    const token = ctx.cookies.get('token')
    const { id } = verify(token)
    if (id) {
      ctx.userid = id;
      await next();
      return;
    }


    ctx.redirect('/login')
  }
}